package com.hrt.manage.role.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Role;
import com.hrt.bean.UserRoles;
import com.hrt.bean.UsersInfor;
import com.hrt.manage.role.service.RoleService;
import com.hrt.manage.role.service.UserRoleService;
import com.hrt.manage.users.service.UserInforService;
import com.hrt.util.PageUtils;
import com.hrt.vo.UserRolesVO;

@Controller
@RequestMapping("manage/userRole")
public class UserRoleController extends BaseController<UserRoles> {

	@Autowired
	private UserInforService userInforService;

	@Autowired
	private UserRoleService userRoleService;

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/listUser")
	public ModelAndView listUser() {
		ModelAndView view = new ModelAndView();
		view.setViewName("manage/access/userList");

		list = userInforService.queryByPage(getPageIndex(), getPageSize());

		setTotalCount(userInforService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		view.addObject("pageResult", page);
		// request.getSession().setAttribute("pageResult", page);
		return view;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryUeryByPage")
	@ResponseBody
	public Map<String, PageUtils<UsersInfor>> queryUeryByPage(

			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map<String, PageUtils<UsersInfor>> resultMap = new HashMap<String, PageUtils<UsersInfor>>();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(userInforService.totalCount());

		List<UsersInfor> resultList = userInforService.queryByPage(
				getPageIndex(), getPageSize());

		int pageCount = this.computePageCount(this.getTotalCount(), pageSize);

		PageUtils<UsersInfor> cur_page = new PageUtils<UsersInfor>(pageSize,
				pageIndex, this.getTotalCount(), pageCount, resultList);

		resultMap.put("pageResult", cur_page);
		return resultMap;
	}

	/**
	 * 查询用户权限
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/showRole")
	@ResponseBody
	public Map<String, Object> showRole(
			@RequestParam(value = "userId") Long userId) {
		List<Role> results = new ArrayList<Role>();
		List<Role> roleList = new ArrayList<Role>();
		UsersInfor user = new UsersInfor();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (userId != null && !"".equals(userId)) {
			roleList = roleService.queryRoleWithOutUsers(userId);
			results = userRoleService.queryRolesByUserId(userId);
			user = userInforService.getUserByid(userId);
		}

		resultMap.put("userRoles", results);
		resultMap.put("roles", roleList);
		resultMap.put("user", user);

		return resultMap;

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("userInfor") UserRolesVO vo,
			HttpServletRequest request, HttpServletResponse response) {

		List<Long> roleList = vo.getRolesId();
		Long userId = vo.getUserId();

		if (roleList != null && !roleList.isEmpty()) {
			this.userRoleService.saveRolesByUserId(userId, roleList);
		}
		try {
			response.sendRedirect("listUser.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
